import { Help } from './help.js';

// 基础物资类
class Material {
    constructor() {
        this.material = 0;
        this.productionRate = 1.01; // 生产率
        this.normal = false; // 是否正常供应
    }

    // 消耗物资
    sub(d) {
        const r = this.material - d;
        if (r <= 0) {
            this.material = 0;
            this.normal = false;
        } else {
            this.material = r;
            this.normal = true;
        }
    }

    // 增加物资
    add(d) {
        this.material += d;
        if (this.material > 0) {
            this.normal = true;
        }
    }

    // 打印状态（调试用）
    print() {
        console.log(`material: ${this.material}`);
        console.log(`productionRate: ${this.productionRate}`);
        console.log(`normal: ${this.normal}`);
        console.log('[finished]');
    }
}

// 生活物资类
class LivingMaterial extends Material {
    constructor(total) {
        super();
        this.startMultiple = 30; // 初始物资为城市总人数的倍数
        this.famineDeath = 0; // 饥荒死亡系数
        this.material = total * this.startMultiple;
        this.normal = true; // 初始状态正常
    }

    // 每日消耗
    updateSub(aliveNum) {
        this.sub(aliveNum);
        if (!this.normal && this.famineDeath < 7) {
            this.famineDeath++;
        }
        if (this.normal && this.famineDeath !== 0) {
            this.famineDeath = 0;
        }
    }

    // 获取饥荒影响比例
    getFamineProp() {
        return this.famineDeath / 7;
    }

    // 每日生产
    updateAdd(aliveNum) {
        this.material += Math.floor(aliveNum * this.productionRate);
        if (this.material > 0) {
            this.normal = true;
        }
    }
}

// 防护物资类
class ProtectiveMaterial extends Material {
    constructor(medicalNum, lm) {
        super();
        this.materialsMultiple = 7; // 初始物资为医生数的倍数
        this.lm = lm; // 关联的生活物资
        this.isProduction = false; // 是否开启生产
        this.material = medicalNum * this.materialsMultiple;
        this.normal = true; // 初始状态正常
    }

    // 每日消耗
    updateSub(hospitalBlock, outpatients, medicalNum, patientTreatRate, outpatientTreatRate) {
        const d1 = Math.floor(hospitalBlock.getKnowInfected() * patientTreatRate);
        const d2 = Math.floor(outpatients * outpatientTreatRate);
        const d = d1 + d2;
        if (d > medicalNum) {
            this.sub(medicalNum);
        } else {
            this.sub(d);
        }
    }

    // 每日生产
    updateAdd(medicalNum) {
        if (this.lm.normal) {
            const productionQuantity = Math.floor(medicalNum * 2 * this.productionRate);
            if (this.isProduction) {
                this.material += productionQuantity;
            }
            this.lm.sub(Math.floor(productionQuantity * 3)); // 防护服消耗三倍资源
        }
    }
}

// 医疗物资类
class MedicalMaterial extends Material {
    constructor(total, lm) {
        super();
        this.lm = lm; // 关联的生活物资
        this.isProduction = false; // 是否开启生产
        this.materialsMultiple = 0.00055; // 生产速度（城市总人口的倍数）
        this.material = Math.floor(total * this.materialsMultiple);
        this.normal = true; // 初始状态正常
    }

    // 每日消耗
    updateSub(hospitalBlock, patientTreatRate, medicalNum) {
        const d = Math.floor(hospitalBlock.getKnowInfected() * patientTreatRate);
        if (d > medicalNum) {
            this.sub(medicalNum);
        } else {
            this.sub(d);
        }
    }

    // 每日生产
    updateAdd(aliveNum) {
        if (this.lm.normal) {
            const productionQuantity = Math.floor(aliveNum * this.materialsMultiple * this.productionRate);
            if (this.isProduction) {
                this.material += productionQuantity;
            }
            this.lm.sub(Math.floor(productionQuantity * 2)); // 药物消耗二倍资源
        }
    }
}

export { Material, LivingMaterial, ProtectiveMaterial, MedicalMaterial };
